import Qust from "/js/Qust.js";
import { $, $$ } from "/js/JSelect.js";

const qust = Qust.create({
	baseUrl: "http://www.rushmc.top/api/",
});

/* 初始执行 */
let url = window.location.search;
if (url.substr(1) == "logout") {
	localStorage.removeItem("uid");
	localStorage.removeItem("uname");
	localStorage.removeItem("upwd");
	localStorage.removeItem("uphone");
	localStorage.removeItem("username");
	localStorage.removeItem("uavatar");
	$$.navigateTo("/html/login.html");
}

/* 事件绑定 */
document.getElementById("toReg").onclick = () => {
	document.getElementById("login").style.animation = "fade-up 0.5s both reverse";
	setTimeout(() => {
		document.getElementById("login").style.display = "none";
		document.getElementById("register").style.animation = "";
		document.getElementById("register").style.display = "block";
	}, 400);
};

function toLogin() {
	document.getElementById("register").style.animation = "fade-up 0.5s both reverse";
	setTimeout(() => {
		document.getElementById("register").style.display = "none";
		document.getElementById("login").style.animation = "";
		document.getElementById("login").style.display = "block";
	}, 400);
}
document.getElementById("toLogin").onclick = toLogin;

// 登录提交
document.getElementById("submitLogin").onclick = () => {
	let username = document.getElementById("username").value;
	let password = document.getElementById("pwd").value;
	if (username == "" || password == "") {
		$$.showModal({
			title: "填写错误",
			content: `请检查你的输入`,
		});
		return;
	}

	qust.request({
		url: "login",
		data: {
			username,
			password,
		},
	})
		.then((res) => {
			if (res.data.code === 200) {
				// 登陆成功,写入基础数据
				$$.showModal({
					title: "登录成功",
					content: `${res.data.msg}\n你好：${res.data.data.name} ${res.data.data.phone}`,
				}).then(() => {
					$$.navigateTo("/html/index.html");
				});
				localStorage.setItem("uid", res.data.data.id);
				localStorage.setItem("uname", res.data.data.name);
				localStorage.setItem("upwd", res.data.data.password);
				localStorage.setItem("uphone", res.data.data.phone);
				localStorage.setItem("username", res.data.data.username);
				localStorage.setItem("uavatar", res.data.data.photo);
			} else {
				$$.showModal({
					title: "登录失败",
					content: `${res.data.msg}`,
				});
			}
		})
		.catch(() => {
			$$.showModal({
				title: "登录错误",
				content: "网络故障",
			});
		});
};

// 注册提交
$("#submitRegister").click(() => {
	let username = $("#regusername").value;
	let password = $("#regpwd").value;
	let name = $("#regname").value;
	let phone = $("#regphone").value;

	if (username == "" || password == "" || !/^(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/.test(phone) || name == "") {
		$$.showModal({
			title: "填写错误",
			content: `请检查你的输入`,
		});
		return;
	}

	qust.request({
		url: "register",
		data: {
			username,
			password,
			name,
			phone,
		},
	})
		.then((res) => {
			if (res.data.code === 200) {
				$$.showModal({
					title: "注册成功",
					content: `${res.data.msg}`,
				});
				toLogin();
			} else {
				$$.showModal({
					title: "注册失败",
					content: `${res.data.msg}`,
				});
			}
		})
		.catch(() => {
			$$.showModal({
				title: "注册错误",
				content: "网络错误",
			});
		});
});
